package org.ttitfly.ttcore.util.helper;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.access.BeanFactoryLocator;
import org.springframework.beans.factory.access.BeanFactoryReference;
import org.springframework.context.access.ContextSingletonBeanFactoryLocator;

public class SpringHelper {
    public static String CONTEXT_NAME = "applicationContext";

    public static void setContextName(String contextName) {
        SpringHelper.CONTEXT_NAME = contextName;
    }

    public static Object getBean(String beanName) {
        return beanFactory().getBean(beanName);
    }

    public static BeanFactory beanFactory() {
        BeanFactoryLocator locator = ContextSingletonBeanFactoryLocator.getInstance();
        BeanFactoryReference ref = locator.useBeanFactory(CONTEXT_NAME);
        BeanFactory beanFactory = ref.getFactory();
        return beanFactory;
    }

}
